//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.DataLayer
{
    using System;
    using System.Collections.Generic;

    [Serializable]
    internal class FilterActionCalls : IDataLayerSecurityActionCalls
    {
        /// <summary>
        /// Filters Available.
        /// </summary>
        private List<IDataLayerSecurityFilter> filters;

        /// <summary>
        /// Initializes a new instance of the <see cref="FilterActionCalls"/> class.
        /// </summary>
        /// <param name="filters">The filters.</param>
        public FilterActionCalls(List<IDataLayerSecurityFilter> filters)
        {
            this.filters = filters;
        }

        #region IDataLayerSecurityActionCalls Members

        /// <summary>
        /// Authenticates the specified error.
        /// </summary>
        /// <param name="error">The error.</param>
        /// <param name="principal">The principal.</param>
        /// <returns></returns>
        public SecurityAction Authenticate(out Exception error, ref ISecurityPrincipal principal)
        {
            error = null;

            // No filters? By default block all traffic
            if (filters == null)
                return SecurityAction.Deny;

            foreach (IDataLayerSecurityFilter filter in filters)
            {
                if (filter.Actions.Authenticate(out error, ref principal) == SecurityAction.Deny)
                    return SecurityAction.Deny;
            }

            return SecurityAction.Allow;
        }

        #endregion
    }
}
